<?php


namespace App\Services\Sms;


use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;

class Aliyun
{

    public function send($receiver, $code)
    {
        return $this->sendCommon($receiver, config('comm.sms.ali.template'), ['code' => $code]);
    }

    public function sendCommon($receiver, $template, $arr)
    {

        $config = [
            'accessKeyId' => config("comm.sms.ali.accessKeyId"),
            'accessKeySecret' => config("comm.sms.ali.accessKeySecret")
        ];

        $client = new Client($config);
        $sendSms = new SendSms();
        $sendSms->setPhoneNumbers($receiver);
        $sendSms->setSignName(config('comm.sms.ali.sign'));
        $sendSms->setTemplateCode($template);
        $sendSms->setTemplateParam($arr);
//        $sendSms->setOutId('demo');

        $response = $client->execute($sendSms);
        $response_decode = json_decode(json_encode($response), 1);

//
        if (json_last_error()) {
            return false;
        }

        if (isset($response_decode['Code']) && strtoupper($response_decode['Code']) == 'OK') {
            return true;
        } else {
            return false;
        }
    }

}
